<?php
namespace app\admin\controller;

class UserController extends CommonController
{
    public $model;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');

    }
    
    /**
     * 会员列表
     */
    public function index()
    {
    	$where['id'] = ['>',0];
    	$mobile = input('get.mobile');
    	if($mobile){
    		$where['mobile'] = ['like','%'.$mobile.'%'];
    	}
    	$email = input('get.email');
    	if($email){
    		$where['email'] = ['like','%'.$email.'%'];
    	}
    	$users = $this->model->where($where)->paginate(20,false,['query' => request()->param()]);
		$page = $users->render();
		$this->assign([
			'users' => $users,
			'page' => $page
		]);
	
		return $this->fetch('index');
    }
    
    /**
     * 会员信息
     */
    public function info()
    {
    	$id = input('param.id');
    	if($id){
    		$info = $this->model->find(['id'=>$id]);
    	}else{
    		$this->error('会员不存在');
    	}
    	if(IS_POST){
    		$data = input('post.');
    		if($data['new_pwd']){
    			if($data['renew_pwd']){
    				if($data['new_pwd']!==$data['renew_pwd']){
    					$this->error('两次输入的密码不一致');
    				}else{
    					$data['password'] = md5($data['new_pwd']);
    				}
    			}else{
    				$this->error('请重复输入密码');
    			}
    		}
    		unset($data['new_pwd']);
    		unset($data['renew_pwd']);
    		if($this->model->where(['id'=>$id])->save($data)!==false){
    			admin_log("更新会员信息，ID：$id");
    		}else{
    			$this->error('操作失败');
    		}
    		$this->redirect('index');
    	}
    	$this->assign([
    			'info' => $info
    	]);
    	return $this->fetch('info');
    }
    
    /**
     * 会员地址列表
     */
    public function address()
    {
    	$id = input('get.id');
    	if(!$user = $this->model->find(['id'=>$id])){
    		$this->error('会员不存在');
    	}
    	$address = db('userAddress')->alias('a')
	    	->join('__REGION__ r','r.id = a.province or r.id = a.city or r.id = a.district','LEFT')
	    	->where(['user_id'=>$id])
	    	->field('a.*,group_concat(r.name SEPARATOR "") as region_address')
	    	->group('a.id')
	    	->select();
    	if(!$address){
    		$this->error('该会员暂无地址');
    	}
    	$this->assign('address',$address);
    	$this->assign('username',$user['username']);
    	return $this->fetch('address');
    }
    
    /**
     * 会员账户资金记录
     */
    public function accountLog()
    {
    	$id = input('param.id');
    	if(!$user = $this->model->find(['id'=>$id])){
    		$this->error('会员不存在');
    	}
    	$model = db('userAccountLog');
    	$logs = $model->where(['user_id'=>$id])->order(['addtime'=>'desc'])->paginate(20,false,['query' => request()->param()]);
		$page = $logs->render();
    	$this->assign([
    		'user' => $user,
    		'logs' => $logs,
    		'page' => $page
    	]);
    	return $this->fetch('accountLog');
    }
    
    /**
     * 编辑账户资金
     */
    public function accountEdit()
    {
    	$id = input('param.id');
    	if(!$user = $this->model->find(['id'=>$id])){
    		$this->error('会员不存在');
    	}
    	if(IS_POST){
    		$data = input('post.');
    		if(!$data['desc']){
    			$this->error('请输入操作备注');
    		}
            $money = $data['money_operate']==1 ? $data['money'] : -$data['money'];
    		$res = db('userAccountLog')->insert([
    			'user_id' => $id,
    			'money' => $money,
    			'integral' => 0,
    			'addtime' => time(),
    			'desc' => $data['desc'].'（管理员'.session('admin.id').'）'
    		]);
    		if($res){
    			db('user')->where(['id'=>$id])->update(['money'=>$user['money']+$money]);
    			$this->redirect('accountLog',['id'=>$id]);
    		}else{
    			$this->error('编辑账户资金发生错误，请重试');
    		}
    	}
    	$this->assign('user',$user);
    	return $this->fetch('accountEdit');
    }
}